﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class BJXXApplicationDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindForms();
            }
        }

        private void BindForms()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            if (!product.ApplicationForm.ApplicationPicture.IsLoaded)
            {
                product.ApplicationForm.ApplicationPicture.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            if (!product.ProblemTypeReference.IsLoaded)
            {
                product.ProblemTypeReference.Load();
            }
            if (product.ApplicationForm.ApplicationType.Equals("BB"))
            {
                applyTypeLabel.Text = "备件先行";
            }
            else if (product.ApplicationForm.ApplicationType.Equals("BD"))
            {
                applyTypeLabel.Text = "DOA";
            }
            if (product.BJSQYBService.HasValue && product.BJSQYBService.Value)
            {
                ybChkBox.Checked = true;
            }
            requireDisPLabel.Text = product.BJSQNeedComment;
            contactLabel.Text = product.ApplicationForm.User.ContactPerson;
            companyLabel.Text = product.ApplicationForm.User.FullName;
            phoneNumberLabel.Text = product.ApplicationForm.User.UserPhone;
            cellPhoneLabel.Text = product.ApplicationForm.User.UserMobilePhone;
            faxLabel.Text = product.ApplicationForm.User.UserFax;
            addressLabel.Text = product.ApplicationForm.User.UserAddress;
            emailLabel.Text = product.ApplicationForm.User.UserEmail;
            agentLabel.Text = product.ApplicationForm.ProviderName;
            Label1.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            Label2.Text = product.DealWithDateTime.HasValue ? product.DealWithDateTime.Value.ToString("yyyy-MM-dd") : "";
            shxxLbl.Text = product.ApplicationForm.BJXXSHAddress;
            //
            runningNumberLabel.Text = product.ApplicationForm.RunningNumber;
            //clientNameLabel.Text = product.ApplicationForm.User.UserName;
            //spcodeLabel.Text = product.SPCode;
            productTypeLabel.Text = product.ProductType.ProductTypeName;
            serialNOLinkButton.Text = product.ProductSerialNumber;
            //applyTimeLabel.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            buyTimeLabel.Text = product.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            statusLabel.Text = product.Status;
            repairTupeLabel.Text = product.ProductRepairType;
            problemDescriptionLabel.Text = product.ProductProblemDescription;
            problemTypeLabel.Text = product.ProblemType.Name;
            accessoryLabel.Text = product.ProductAppearance;
            versionNumberLabel.Text = product.ProductVersionNumber;
            remarkLabel.Text = product.BJXXApproveComment;
            applyDateLbl.Text = product.ApplicationForm.ApplicationDateTime.HasValue?product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd"):"";
            dealDateLbl.Text = product.DealWithDateTime.HasValue ? product.DealWithDateTime.Value.ToString("yyyy-MM-dd HH:mm") : "";
            sendDateLbl.Text = product.BJXXSendToClient.HasValue ? product.BJXXSendToClient.Value.ToString("yyyy-MM-dd HH:mm") : "";
            returnDateLbl.Text = product.BJXXReturnBack.HasValue ? product.BJXXReturnBack.Value.ToString("yyyy-MM-dd HH:mm") : "";
            receiveDateLbl.Text = product.BJXXClientReceive.HasValue ? product.BJXXClientReceive.Value.ToString("yyyy-MM-dd HH:mm") : "";

            UserService userservice = new UserService();
            if (product.ApplicationForm.AgentID.HasValue)
            {
                User agent = userservice.GetUserByID(product.ApplicationForm.AgentID.Value);
                Label3.Text = agent.ContactPerson;
                Label4.Text = agent.UserPhone;
            }
            if (product.DealUserID.HasValue)
            {
                User dealuser = userservice.GetUserByID(product.DealUserID.Value);
                dealuserLabel.Text = dealuser.ContactPerson;
            }

            sendProducttypeLabel.Text = product.ReplaceProductType;
            sendSerialNumberLabel.Text = product.ReplaceProductSerialNumber;
            sendVersinNumberLabel.Text = product.ReplaceProductVersion;
            returnSerialNumberLabel.Text = product.ReturnSerialNumber;
            returnTypeLabel.Text = product.ReturnProductType;
            returnremarkLabel.Text = product.BJXXClientReceiveMark;
            // pictures            
            List<ApplicationPicture> list = product.ApplicationForm.ApplicationPicture.ToList();
            Table table = new Table();
            table.Rows.Add(new TableRow());
            table.Rows.Add(new TableRow());
            foreach (ApplicationPicture picture in list)
            {
                string docType = GetDocType(picture.PictureNameOnServer);
                TableCell celllImage = new TableCell();
                Image imageControl = new Image();
                imageControl.Height = 100;
                if (docType.ToLower() != "jpg" && docType.ToLower() != "bmp" && docType.ToLower() != "gif" && docType.ToLower() != "png")
                {
                    imageControl.ImageUrl = "image/" + "document.png";
                    imageControl.Attributes.Add("OnClick", "window.open('Picture/" + picture.PictureNameOnServer + "')");
                }
                else
                {
                    imageControl.ImageUrl = "Picture/" + picture.PictureNameOnServer;
                    imageControl.Attributes.Add("OnClick", "window.open('ImageDetail.aspx?Name=" + picture.PictureNameOnServer + "')");
                }

                celllImage.Controls.Add(imageControl);
                table.Rows[0].Cells.Add(celllImage);

                TableCell cellName = new TableCell();
                Label label = new Label();
                label.Text = picture.PictureName;
                cellName.Controls.Add(label);
                table.Rows[1].Cells.Add(cellName);
            }
            picturePanel.Controls.Add(table);
        }

        private string GetDocType(string nameonserver)
        {
            if (!string.IsNullOrEmpty(nameonserver))
            {
                string[] nametype = nameonserver.Split('.');
                return nametype[1];
            }
            else
            {
                return string.Empty;
            }
        }

        protected void serialNOLinkButton_Click(object sender, EventArgs e)
        {
            LinkButton serialNOLinkButton = sender as LinkButton;
            ProductService service = new ProductService();
            ProductInfo pi = service.GetProductInfoBySerialNO(serialNOLinkButton.Text);
            if (pi != null)
            {
                serialNOLabel.Text = pi.SerialNO;
                saleUserLabel.Text = pi.JXSName;
                endtimeLabel.Text = pi.EndTime.HasValue ? pi.EndTime.Value.ToString("yyyy-MM-dd") : "";
                toLabel.Text = pi.SellTime.HasValue ? pi.SellTime.Value.ToString("yyyy-MM-dd") : "";
                informationLabel.Visible = false;
            }
            else
            {
                informationLabel.Visible = true;
                informationLabel.Text = "没有找到相关信息！";
            }

            notificationModalPopupExtender.Show();
        }
    }
}
